<template>
  <el-drawer v-model="drawer" title="编辑收款" size="50%">
    <el-form
      :rules="formRules"
      ref="formRef"
      :model="divideResult"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="收款金额" prop="dividePriceCNY">
        {{ divideResult.dividePriceCNY }}
      </el-form-item>
      <el-form-item label="收款人" prop="userName">
        {{ divideResult.userName }}
      </el-form-item>
      <el-form-item label="银行" prop="bankName">
        {{ divideResult.bankName }}
      </el-form-item>
      <el-form-item label="银行卡号" prop="card">
        {{ divideResult.card }}
      </el-form-item>
      <el-form-item label="手机号" prop="avatar">
        {{ divideResult.phone }}
      </el-form-item>
      <el-form-item label="付款凭证" prop="payImgUrl">
        <el-upload
          class="avatar-uploader"
          :action="url"
          :show-file-list="false"
          :on-success="handlePayImgSuccess"
          :before-upload="beforeUpload"
          :headers="headers"
        >
          <img v-if="divideResult.payImgUrl" :src="divideResult.payImgUrl" class="avatar" />
          <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
        </el-upload>
      </el-form-item>
      <el-form-item label="完税证明" prop="taxImgUrl">
        <el-upload
          class="avatar-uploader"
          :action="url"
          :show-file-list="false"
          :on-success="handleTaxImgSuccess"
          :before-upload="beforeUpload"
          :headers="headers"
        >
          <img v-if="divideResult.taxImgUrl" :src="divideResult.taxImgUrl" class="avatar" />
          <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
        </el-upload>
      </el-form-item>
      <el-form-item label="付款状态" prop="status">
        <el-select v-model="divideResult.status" placeholder="请选择" clearable>
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.MEIJIA_DIVIDE_RESULT_STATUS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="失败原因" prop="remark">
        <el-input v-model="divideResult.remark" placeholder="请输入" type="textarea"
      /></el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="drawer = false">取 消</el-button>
    </template>
  </el-drawer>
</template>
<script setup lang="ts">
import { DICT_TYPE, getIntDictOptions, getStrDictOptions } from '@/utils/dict'
import { dateFormatter } from '@/utils/formatTime'
import type { UploadProps } from 'element-plus'
import { ElMessage } from 'element-plus'
import { Plus } from '@element-plus/icons-vue'
import * as DivideApi from '@/api/meijia/divide'
import { getAccessToken, getTenantId } from '@/utils/auth'
import { da } from 'element-plus/es/locale'

const url = import.meta.env.VITE_UPLOAD_URL
const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗
const dialogVisible = ref(false) // 弹窗的是否展示
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const drawer = ref(false)
/** 打开弹窗 */
const open = async (code: string) => {
  drawer.value = true
  await getDivideResult(code)
}
const divideResult = ref({
  dividePrice: undefined,
  name: undefined,
  phone: undefined,
  bankName: undefined,
  card: undefined,
  payImgUrl: undefined,
  taxImgUrl: undefined,
  status: undefined
})
const getDivideResult = async (code: string) => {
  const param = { code: code }
  divideResult.value = await DivideApi.getResultCommonResult(param)
}
const formRules = reactive({
  payImgUrl: [{ required: true, message: '付款凭证不能为空', trigger: 'blur' }],
  status: [{ required: true, message: '付款状态不能为空', trigger: 'blur' }]
})
defineExpose({ open }) // 提供 open 方法，用于打开弹窗
const formRef = ref() // 表单 Ref
/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = divideResult.value
    await DivideApi.updateResultByCode(data)
    drawer.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}
const headers = ref({
  Authorization: 'Bearer ' + getAccessToken()
})
const handlePayImgSuccess: UploadProps['onSuccess'] = (response, uploadFile) => {
  divideResult.value.payImgUrl = response.data
}
const handleTaxImgSuccess: UploadProps['onSuccess'] = (response, uploadFile) => {
  divideResult.value.taxImgUrl = response.data
}
const beforeUpload: UploadProps['beforeUpload'] = (rawFile) => {
  if (rawFile.type !== 'image/jpeg' && rawFile.type !== 'image/png' ) {
    ElMessage.error('Avatar picture must be JPG PNG format!')
    return false
  } else if (rawFile.size / 1024 / 1024 > 5) {
    ElMessage.error('上传照片不能超过5MB!')
    return false
  }
  return true
}
</script>
<style>
.el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 78px;
  height: 78px;
  text-align: center;
}
</style>
<style scoped>
img {
  width: 150px;
}
</style>
